package com.kotlin.book.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.kotlin.book.databinding.ItemDetailCommentBinding
import com.kotlin.book.entry.Comment
import com.kotlin.book.util.Util

class DetailCommentItemAdapter(
private var itemList: List<Comment>
) : RecyclerView.Adapter<DetailCommentItemAdapter.ViewHolder>() {

    class ViewHolder(itemBinding: ItemDetailCommentBinding) :
        RecyclerView.ViewHolder(itemBinding.root) {
        val commentNickname = itemBinding.commentNickname
        val commentTime = itemBinding.commentTime
        val commentAvatar = itemBinding.commentAvatar
        val commentContent = itemBinding.commentContent
        val chapterIndex = itemBinding.chapterIndex
        val review = itemBinding.review
        val praise = itemBinding.praise
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding =
            ItemDetailCommentBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return itemList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = itemList[position]
        holder.commentNickname.text = item.userNickname
        holder.commentTime.text = item.createTime
        holder.commentContent.text = item.content
        holder.chapterIndex.text = item.chapterIndex?.let {
            (it+1).toString()
        }
        holder.review.text = item.replyCount.toString()
        holder.praise.text = item.praiseCount.toString()
        Util.getImage(item.userAvatar,holder.commentAvatar,holder.itemView.context)
    }

    fun setData(data: List<Comment>) {
        itemList = data
        notifyDataSetChanged()
    }
}